<?php
/**
 *
 * @category   JND
 * @package    Ddb_View
 * @subpackage Helper
 * @copyright  Copyright (c) 2005-2009 DDB
 * @version    $Id: FormAutocomplete.php 16541 2009-10-12 17:02:30Z tbrouard $
 */
/**
 * Abstract class for extension
 */
require_once 'Zend/View/Helper/FormElement.php';
/**
 * Helper to generate a "hidden" element
 *
 * @category   Jnd
 * @package    Jnd_View
 * @subpackage Helper
 */
class Jnd_View_Helper_FormAutocomplete extends Zend_View_Helper_FormElement
{

    /**
     * Generates a 'hidden' element.
     *
     * @access public
     *
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are extracted in place of added parameters.
     * @param mixed $value The element value.
     * @param array $attribs Attributes for the element tag.
     * @return string The element XHTML. div tag + input hidden tag
     */
    public function formAutocomplete($name, $value = null, array $attribs = null)
    {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable
        if (isset($id)) {
            if (isset($attribs) && is_array($attribs)) {
                $attribs['id'] = $id;
            } else {
                $attribs = array('id' => $id);
            }
        } else {
            $attribs['id'] = $name;
        }
        $attribs['name'] = $name;
        // Defaults attribs
        $defaultAttribs = array(
            'url' => '/ajax/personnesgroupesldap/format/json/',
            'dataType' => "json",
            'successFunction' => 'autocompletePersonnesGroupes',
            'minLength' => '3');
        $attribs = array_merge($defaultAttribs, $attribs);
        if (isset($attribs['select'])) {
            $select = "select: " . $attribs['select'] . ",";
        } else {
            $select = "";
        }
        if (isset($attribs['vidage']) && $attribs['vidage'] == true) {
            $vidage = "$(this).val('');";
        } else {
            $vidage = "";
        }
        if (isset($attribs['helper']) && is_array($attribs['helper'])) {
            $imgHelper = "<img src='" . $attribs['helper']['image'] . "' alt='' border='0' onclick='" . $attribs['helper']['ajaxCallback'] . "' />";
            $cibleHelper = $attribs['helper']['cible'];
        }
        $view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource(
            "view");
        $jquery = Jnd_App::getConfig('jquery');
        $jqueryServeurUrl = $jquery['path']['min'];
        $view->jQuery()->addJavascriptFile('/js/autocompleteAppli.js');
//         $view->jQuery()->addStylesheet(
//             $jqueryServeurUrl . 'css/dark-hive/jquery-ui-1.8.Autocomplete.Datepicker.css');
        $view->jQuery()->addOnLoad(
            "$('#" . $attribs['id'] . "').autocomplete({
      'source': function(request, response) {
          $.ajax({
            url: '" . $attribs["url"] . "',
            dataType: '" . $attribs["dataType"] . "',
            data: {
              q: request.term
            },
            success: function(data) {
              response($.map(data.resultat, " . $attribs["successFunction"] . "))
            }
          })
        },
          minLength: " . $attribs["minLength"] . ",
          " . $select . "
          open: function() {
          $(this).removeClass('ui-corner-all').addClass('ui-corner-top');
        },
        close: function() {
          $(this).removeClass('ui-corner-top').addClass('ui-corner-all');
          " . $vidage . "
        }
      });");
        $helper = new Zend_View_Helper_FormText();
        $helper->setView($view);
        //  On supprime ce qui n'est pas html
        $aSupprimer = array('url', 'dataType',
            'successFunction');
        foreach ($aSupprimer as $aEff) {
            unset($attribs[$aEff]);
        }
        return $helper->formText($attribs['id'], $value, $attribs);
    }
}
